package com.ruoyi.pos.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.pos.domain.PosMenu;
import com.ruoyi.pos.service.IPosMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * POS菜单配置Controller
 * 
 * @author Grand Pacific
 * @date 2024-01-01
 */
@Api(tags = "POS菜单配置管理")
@RestController
@RequestMapping("/pos/menu")
public class PosMenuController extends BaseController {
    
    @Autowired
    private IPosMenuService posMenuService;

    /**
     * 查询POS菜单配置列表
     */
    @ApiOperation("查询POS菜单配置列表")
    @PreAuthorize("@ss.hasPermi('pos:menu:list')")
    @GetMapping("/list")
    public TableDataInfo list(PosMenu posMenu) {
        startPage();
        List<PosMenu> list = posMenuService.selectPosMenuList(posMenu);
        return getDataTable(list);
    }

    /**
     * 获取POS菜单配置详细信息
     */
    @ApiOperation("获取POS菜单配置详细信息")
    @PreAuthorize("@ss.hasPermi('pos:menu:query')")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@ApiParam("菜单ID") @PathVariable("menuId") Long menuId) {
        return success(posMenuService.selectPosMenuByMenuId(menuId));
    }

    /**
     * 新增POS菜单配置
     */
    @ApiOperation("新增POS菜单配置")
    @PreAuthorize("@ss.hasPermi('pos:menu:add')")
    @PostMapping
    public AjaxResult add(@RequestBody PosMenu posMenu) {
        return toAjax(posMenuService.insertPosMenu(posMenu));
    }

    /**
     * 修改POS菜单配置
     */
    @ApiOperation("修改POS菜单配置")
    @PreAuthorize("@ss.hasPermi('pos:menu:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody PosMenu posMenu) {
        return toAjax(posMenuService.updatePosMenu(posMenu));
    }

    /**
     * 删除POS菜单配置
     */
    @ApiOperation("删除POS菜单配置")
    @PreAuthorize("@ss.hasPermi('pos:menu:remove')")
    @DeleteMapping("/{menuIds}")
    public AjaxResult remove(@ApiParam("菜单ID数组") @PathVariable Long[] menuIds) {
        return toAjax(posMenuService.deletePosMenuByMenuIds(menuIds));
    }

    /**
     * 获取POS设备可用菜单列表（不需要权限，POS设备调用）
     * 根据设备类型、设备编码等条件返回该设备可显示的菜单
     */
    @ApiOperation("获取POS设备可用菜单列表")
    @GetMapping("/device/menus")
    public AjaxResult getDeviceMenus(
            @ApiParam("设备编码") @RequestParam(required = false) String deviceCode,
            @ApiParam("设备类型") @RequestParam(required = false) String deviceType) {
        List<PosMenu> menus = posMenuService.selectDeviceMenus(deviceCode, deviceType);
        return success(menus);
    }

    /**
     * 获取POS菜单树形结构
     */
    @ApiOperation("获取POS菜单树形结构")
    @PreAuthorize("@ss.hasPermi('pos:menu:list')")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(PosMenu posMenu) {
        List<PosMenu> menus = posMenuService.selectPosMenuList(posMenu);
        return success(posMenuService.buildMenuTreeSelect(menus));
    }

    /**
     * 根据父级菜单ID获取子菜单列表
     */
    @ApiOperation("根据父级菜单ID获取子菜单列表")
    @GetMapping("/children/{parentId}")
    public AjaxResult getChildren(@ApiParam("父级菜单ID") @PathVariable Long parentId) {
        List<PosMenu> children = posMenuService.selectMenusByParentId(parentId);
        return success(children);
    }

    /**
     * 更新菜单状态（启用/禁用）
     */
    @ApiOperation("更新菜单状态")
    @PreAuthorize("@ss.hasPermi('pos:menu:edit')")
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody PosMenu posMenu) {
        return toAjax(posMenuService.updatePosMenuStatus(posMenu));
    }

    /**
     * 更新菜单排序
     */
    @ApiOperation("更新菜单排序")
    @PreAuthorize("@ss.hasPermi('pos:menu:edit')")
    @PutMapping("/changeOrder")
    public AjaxResult changeOrder(@RequestBody PosMenu posMenu) {
        return toAjax(posMenuService.updatePosMenuOrder(posMenu));
    }
}
